package com.sgb.kjwl.view.ui.adapter.pick;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.sgb.kjwl.R;
import com.swgk.core.recyclerview.secondary.CommonHolder;
import com.swgk.core.util.ScreenUtil;

/**
 * Created by wj on 2021/2/5 0005
 * Description：
 */
public class PickHomeHotChildHeadAdapter extends RecyclerView.Adapter {

    private Context mContext;
    private int TYPE_HEAD = 1;
    private int TYPE_ITEM = 2;
    private String[] headData;
    private String[][] itemData;
    protected OnItemClick onItemClick;
    private int positionMoney = -1;
    private int isDisplay = -1;//0、隐藏；1、展示；

    public PickHomeHotChildHeadAdapter(Context context, String[] headData, String[][] itemData) {
        this.mContext = context;
        this.headData = headData;
        this.itemData = itemData;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder = CommonHolder.get(mContext, parent, R.layout.item_pick_home_hot_head);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (position == 0) {
            initHead((CommonHolder)holder);
        } else {
            initItem((CommonHolder)holder,position);
        }

        ((CommonHolder) holder).getItem().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClick != null) {
                    onItemClick.onItemClick();
                }
            }
        });
    }

    private void initHead(CommonHolder holder) {
        LinearLayout linearLayout = holder.getItem().findViewById(R.id.head_layout);
        linearLayout.removeAllViews();
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1);
        for (int i = 0; i < headData.length; i++) {
            if(isDisplay == 0){
                if(headData[i].contains("最低单价(元)") || headData[i].contains("最低报价(元)") || headData[i].contains("最高单价(元)") || headData[i].contains("最高报价(元)")){
                    positionMoney = i;
                    break;
                }
            }
            TextView tv = new TextView(mContext);
            tv.setTextSize(13);
            tv.setText(headData[i]);
            tv.setTextColor(mContext.getResources().getColor(R.color.color_333333));
            tv.setSingleLine();
            tv.setEllipsize(TextUtils.TruncateAt.END);
            tv.setGravity(Gravity.LEFT);
            tv.setPadding(0, 0, 15, 0);
            tv.setLayoutParams(layoutParams);
            linearLayout.addView(tv);
        }

    }

    private void initItem(CommonHolder holder,int position) {
        LinearLayout linearLayout = holder.getItem().findViewById(R.id.head_layout);
        linearLayout.removeAllViews();
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1);
        layoutParams.topMargin = ScreenUtil.dip2px(5);
        for (int i = 0; i < itemData[position - 1].length; i++) {
            if(i == positionMoney){
                break;
            }
            TextView tv = new TextView(mContext);
            tv.setTextSize(12);
            if (TextUtils.isEmpty(itemData[position - 1][i])) {
                tv.setText("/");
            } else {
                tv.setText(itemData[position - 1][i]);
            }
            tv.setTextColor(mContext.getResources().getColor(R.color.color_666666));
            tv.setSingleLine();
            tv.setEllipsize(TextUtils.TruncateAt.END);
            tv.setGravity(Gravity.LEFT);
            tv.setPadding(0, 0, 15, 0);
            tv.setLayoutParams(layoutParams);
            linearLayout.addView(tv);
        }

    }

    @Override
    public int getItemViewType(int position) {
        if(position == 0){
            return TYPE_HEAD;
        }
        return TYPE_ITEM;
    }

    @Override
    public int getItemCount() {
        return itemData.length + 1;
    }

    public interface OnItemClick {
        void onItemClick();
    }

    public void setDisplay(int isDisplays) {
        isDisplay = isDisplays;
    }

    public void setOnItemClick(OnItemClick onItemClick) {
        this.onItemClick = onItemClick;
    }
}
